package com.yz.smartpos.yztester.ui;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;


import androidx.appcompat.app.AppCompatActivity;

import com.yz.smartpos.yztester.R;
import com.yz.smartpos.yztester.util.ApiTestCase;
import com.yz.smartpos.yztester.util.ContextUtil;
import com.yz.smartpos.yztester.util.Utils;
import com.yz.smartpos.yztester.util.YzTestRunner;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public static Context mContext;
    public static ResultFragment mMsgLogFragment;
    public static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreateView");
        setContentView(R.layout.activity_main);

        mContext = this;
        YzTestRunner.setActivity(this);
        ContextUtil.setActivity(this);
        ApiTestCase.setActivity(this);
        Utils.setActivity(this);

        mMsgLogFragment = ResultFragment.newInstance();

        initPermission();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 检查这个包名的app是否存在
     *
     * @param packageName
     * @return
     */
    private boolean checkApkExist(String packageName) {
        if (packageName == null || "".equals(packageName)) {
            return false;
        }
        try {
            getPackageManager().getApplicationInfo(
                    packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    private void initPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] permissions = {
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.CAMERA
            };
            requestPermissions(permissions, 100);
            ArrayList<String> toApplyList = new ArrayList<String>();
            for (String perm : permissions) {
                if (PackageManager.PERMISSION_GRANTED != checkSelfPermission(perm)) {
                    toApplyList.add(perm);
                }
            }
            String[] tmpList = new String[toApplyList.size()];
            if (!toApplyList.isEmpty()) {
                requestPermissions(toApplyList.toArray(tmpList), 100);
            }

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        if (requestCode == 100) {
            if (grantResults != null && grantResults.length > 0) {
                int i = 0;
                for (int grantResult : grantResults) {
                    if (grantResult == PackageManager.PERMISSION_GRANTED)
                        Log.e(TAG, permissions[i]+ " ok"); // 权限被用户同意，可以做你要做的事情了。
                    else
                        Log.e(TAG, permissions[i]+ " fail"); // 权限被用户拒绝了，可以提示用户,关闭界面等等。
                    i ++;
                }
            } else {
                Log.e(TAG, "xxxx");
                // 权限被用户拒绝了，可以提示用户,关闭界面等等。
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");

    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }
}
